package servlets;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import managers.DatabaseManager;

public class SetADBoughtServlet extends HttpServlet {

	private static final long serialVersionUID = -5130044702301226113L;

	protected void doPut(HttpServletRequest request, HttpServletResponse response) {
		String idAdvertisement, idUser;
		boolean error;
		DatabaseManager databaseManager;
		databaseManager = new DatabaseManager();
		idAdvertisement = request.getParameter("idAdvertisement");
		idUser = request.getParameter("idUser");
         try {
        	databaseManager.connect();
        	error = databaseManager.SetADBought(idAdvertisement, idUser);
        	response.setContentType("application/json");
    		response.setCharacterEncoding("UTF-8");
    		if(!error)
    			response.getWriter().write("{\n\t\"errors\":\"no\"\n}");
    		else
    			response.getWriter().write("{\n\t\"errors\":\"yes\"\n}");
			databaseManager.closeConnection();
	        
        } catch (Exception e) {
			e.printStackTrace();
			try {
				response.getWriter().write("{\n\t\"errors\":\"yes\"\n}");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
    }
}
